namespace SimpleWebServices.Web.UI.Html.Validation
{
    using System.Text.RegularExpressions;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Web.UI.Html.Elements;
    using Xunit;

    public sealed class EmailTextValidatorFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<EmailTextValidator>()
                .DerivesFrom<RegexTextValidator>()
                .IsConcreteClass()
                .IsUnsealed()
                .HasDefaultConstructor()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new EmailTextValidator());
        }

        [Fact]
        public void ctor_IInnerElements()
        {
            Assert.NotNull(new EmailTextValidator(new Label()));
        }

        [Fact]
        public void ctor_IInnerElementsNull()
        {
            Assert.NotNull(new EmailTextValidator(null as IInnerElements));
        }

        [Fact]
        public void op_Configure_Form_DataCollection()
        {
            DataCollection data = new DataCollection();
            data.Add("Display", "display");
            data.Add("ErrorText", "error");

            Label label = new Label { Id = "display" };
            Form form = new Form
            {
                Elements = { label }
            };

            EmailTextValidator obj = new EmailTextValidator();
            obj.Configure(form, data);

            Assert.Same(label, obj.Display);
            Assert.Equal<string>("error", obj.ErrorText);
        }

        [Fact]
        public void op_Configure_Form_DataCollectionEmpty()
        {
            new EmailTextValidator().Configure(new Form(), new DataCollection());
        }

        [Fact]
        public void op_Configure_Form_DataCollectionNull()
        {
            new EmailTextValidator().Configure(new Form(), null as DataCollection);
        }

        [Fact]
        public void op_Validate_IElement_whenValid()
        {
            TextInput element = new TextInput
            {
                Value = "john.doe+test@example.com"
            };

            Assert.True(new EmailTextValidator().Validate(element));
        }

        [Fact]
        public void op_Validate_IElement_whenInvalidStringEmpty()
        {
            string error = "Error text";

            TextInput element = new TextInput
            {
                Value = string.Empty
            };

            EmailTextValidator obj = new EmailTextValidator
            {
                ErrorText = error
            };

            Assert.False(obj.Validate(element));

            Assert.Equal<int>(1, obj.Errors.Count);
            Assert.Equal<string>(error, obj.Errors[0].Description);
        }

        [Fact]
        public void op_Validate_IElement_whenInvalid()
        {
            string error = "Error text";

            TextInput element = new TextInput
            {
                Value = "ABC...123"
            };

            EmailTextValidator obj = new EmailTextValidator
            {
                Regex = new Regex("^[a-zA-Z0-9]+$"),
                ErrorText = error
            };

            Assert.False(obj.Validate(element));

            Assert.Equal<int>(1, obj.Errors.Count);
            Assert.Equal<string>(error, obj.Errors[0].Description);
        }
    }
}